package com.web.systemParam.service;

import com.web.account.entity.Account;
import com.web.account.mapper.AccountMapper;
import com.web.common.BaseResult;
import com.web.common.PendingAmend;
import com.web.common.enums.BaseStatus;
import com.web.pendingList.entity.PendingList;
import com.web.pendingList.service.PendingListService;
import com.web.systemParam.entity.SystemParam;
import com.web.systemParam.mapper.SystemParamMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * @author wenzhuo
 * @create 2024-01-09 2024/1/9
 */
@Service
@Transactional
public class SystemParamService implements PendingAmend {

    @Resource
    private SystemParamMapper systemParamMapper;

    @Resource
    private PendingListService pendingService;


    public List<SystemParam> getSystemParamList(String systemParamName, String systemParamType){
       return systemParamMapper.getSystemParams(systemParamName,systemParamType);
    }

    public Integer addSystemParam(SystemParam systemParam){
        return systemParamMapper.addSystemParam(systemParam);
    }

    public Integer updateSystemParam(SystemParam systemParam){
       return systemParamMapper.amendSystemParam(systemParam);
    };

    public Integer deleteSystemParam(String systemParamName){
        return systemParamMapper.logicalDeleteSystemParam(systemParamName);
    };

    public SystemParam checkDuplicateData(String systemParamName) {
        return  systemParamMapper.checkRepeat(systemParamName);
    }

    public Integer updatePendingNumber(SystemParam systemParamName) {
        return systemParamMapper.updatePendingNumber(systemParamName);
    }

    public Integer addTemporaryData(SystemParam systemParamNew) {
        return systemParamMapper.addTemporarySystemParam(systemParamNew);
    }

    @Override
    public BaseResult rejectAddPending(PendingList pendingTxn) {
        //根据PendingNumber查询出SystemParam数据
        SystemParam systemParam = systemParamMapper.getSystemParamByPendingNumber(pendingTxn.getPendingNum());

        //将Pending记录的状态改为REJ.
        if(pendingService.updatePendingTxn(PendingList.makeReject(pendingTxn)) < 0){
            return new BaseResult(BaseStatus.PENDING__DATA_EXCEPTION.getCode(),BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
        }

        //将SystemParam数据进行物理删除.
        if(systemParamMapper.physicsDeleteByPendingNumber(systemParam.getPendingNumber()) <= 0){
            return new BaseResult(BaseStatus.SYSTEM_PARAM_DATA_EXCEPTION.getCode(),BaseStatus.SYSTEM_PARAM_DATA_EXCEPTION.getDesc());
        }
        return new BaseResult(BaseStatus.SUCCESS.getCode(),BaseStatus.PENDING_REJECT_SUCCESS.getDesc());
    }

    @Override
    public BaseResult approveAddPending(PendingList pendingTxn) {
        //根据PendingNumber查询出SystemParam数据
        SystemParam systemParam = systemParamMapper.getSystemParamByPendingNumber(pendingTxn.getPendingNum());

        //将Pending记录的状态改为APP.
        if(pendingService.updatePendingTxn(PendingList.makeApprove(pendingTxn)) < 0){
            return new BaseResult(BaseStatus.PENDING__DATA_EXCEPTION.getCode(),BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
        }

        //将SystemParam数据的APPROVED标记为Y,PendingNumber设置为NULL
        if(systemParamMapper.updateSystemParamApproved(systemParam.getPendingNumber()) <= 0){
            return new BaseResult(BaseStatus.SYSTEM_PARAM_DATA_EXCEPTION.getCode(),BaseStatus.SYSTEM_PARAM_DATA_EXCEPTION.getDesc());
        }

        return new BaseResult(BaseStatus.SUCCESS.getCode(),BaseStatus.PENDING_APPROVE_SUCCESS.getDesc());
    }

    @Override
    public BaseResult rejectDeletePending(PendingList pendingTxn) {
        //根据PendingNumber查询出SystemParam数据
        SystemParam systemParam = systemParamMapper.getSystemParamByPendingNumber(pendingTxn.getPendingNum());

        //将Pending记录的状态改为REJ.
        if(pendingService.updatePendingTxn(PendingList.makeReject(pendingTxn)) < 0){
            return new BaseResult(BaseStatus.PENDING__DATA_EXCEPTION.getCode(),BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
        }

        //将SystemParam数据的PendingNumber设置为NULL
        systemParam.setPendingNumber(null);
        if(systemParamMapper.updatePendingNumber(systemParam) <= 0){
            return new BaseResult(BaseStatus.SYSTEM_PARAM_DATA_EXCEPTION.getCode(),BaseStatus.SYSTEM_PARAM_DATA_EXCEPTION.getDesc());
        }

        return new BaseResult(BaseStatus.SUCCESS.getCode(),BaseStatus.PENDING_REJECT_SUCCESS.getDesc());
    }

    @Override
    public BaseResult approveDeletePending(PendingList pendingTxn) {
        //将Pending记录的状态改为APP.
        if(pendingService.updatePendingTxn(PendingList.makeApprove(pendingTxn)) < 0){
            return new BaseResult(BaseStatus.PENDING__DATA_EXCEPTION.getCode(),BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
        }

        //物理删除SystemParam数据
        if(systemParamMapper.physicsDeleteByPendingNumber(pendingTxn.getPendingNum()) <= 0){
            return new BaseResult(BaseStatus.SYSTEM_PARAM_DATA_EXCEPTION.getCode(),BaseStatus.SYSTEM_PARAM_DATA_EXCEPTION.getDesc());
        }

        return new BaseResult(BaseStatus.SUCCESS.getCode(),BaseStatus.PENDING_APPROVE_SUCCESS.getDesc());
    }

    @Override
    public BaseResult rejectModifyPending(PendingList pendingTxn) {

        //根据PendingNumber查询出SystemParamOld数据
        SystemParam systemParamOld = systemParamMapper.getSystemParamByPendingNumber(pendingTxn.getPendingNum());

        //将PendingTxn的状态修改为REJ
        if(pendingService.updatePendingTxn(PendingList.makeReject(pendingTxn)) < 0){
            return new BaseResult(BaseStatus.PENDING__DATA_EXCEPTION.getCode(),BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
        }

        //物理删除临时表中的数据
        if(systemParamMapper.physicsDeleteAccountTmp(systemParamOld.getPendingNumber()) <= 0){
            return new BaseResult(BaseStatus.FCE_DELETE_FAIL.getCode(),BaseStatus.FCE_DELETE_FAIL.getDesc());
        }

        //将systemParamOld数据的PendingNumber设置为null
        if(systemParamMapper.updateSystemParamApproved(systemParamOld.getPendingNumber()) <= 0){
            return new BaseResult(BaseStatus.FCE_UPDATE_FAIL.getCode(),BaseStatus.FCE_UPDATE_FAIL.getDesc());

        }

        return new BaseResult(BaseStatus.SUCCESS.getCode(),BaseStatus.PENDING_REJECT_SUCCESS.getDesc());
    }

    @Override
    public BaseResult approveModifyPending(PendingList pendingTxn) {
        //根据PendingNumber查询出systemParamOld数据
        SystemParam systemParamOld = systemParamMapper.getSystemParamByPendingNumber(pendingTxn.getPendingNum());

        //根据PendingNumber查询出systemParamNew数据
        SystemParam systemParamNew = systemParamMapper.getTemporarySystemParam(pendingTxn.getPendingNum());

        //将PendingTxn的状态修改为APP
        if(pendingService.updatePendingTxn(PendingList.makeApprove(pendingTxn)) < 0){
            return new BaseResult(BaseStatus.PENDING__DATA_EXCEPTION.getCode(),BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
        }

        //修改systemParamOld的数据
        systemParamOld.setRemark(systemParamNew.getRemark());
        systemParamOld.setSystemParamValue(systemParamNew.getSystemParamValue());

        if(systemParamMapper.updateSourceSystemParam(systemParamOld) <= 0){
            return new BaseResult(BaseStatus.ACCOUNT_DATA_EXCEPTION.getCode(),BaseStatus.ACCOUNT_DATA_EXCEPTION.getDesc());
        };

        //物理删除临时表中的数据
        if(systemParamMapper.physicsDeleteAccountTmp(systemParamOld.getPendingNumber()) <= 0){
            return new BaseResult(BaseStatus.FCE_DELETE_FAIL.getCode(),BaseStatus.FCE_DELETE_FAIL.getDesc());
        }

        return new BaseResult(BaseStatus.SUCCESS.getCode(),BaseStatus.PENDING_APPROVE_SUCCESS.getDesc());
    }


    public static boolean isAfterDays(LocalDate date,Long days) {
        LocalDate now = LocalDate.now();
        LocalDate daysLater = now.plusDays(days);
        return date.isAfter(daysLater);
    }

}
